package org.webshop.frontend.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.webshop.backend.data.Ingredient;
import org.webshop.backend.data.Product;
import org.webshop.backend.dbchif.DbAccess;
import org.webshop.frontend.servlets.util.ServletUtil;

@WebServlet("/admin/products/addIngredient")
public class ProductAddIngredientServlet extends HttpServlet {

    /**
     * Serve the system manager a view for adding a product
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletUtil.forwardToView(request, response, "addProduct");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        int productId = Integer.parseInt(request.getParameter("productId"));
        int ingredientId = Integer.parseInt(request.getParameter("ingredientId"));

        Product product = new Product(productId);
        Ingredient ingredient = new Ingredient(ingredientId);
        DbAccess dbAccess = DbAccess.getInstance();
        dbAccess.addIngredientToProduct(product, ingredient);
    }
}

